package leetcode_901_1000;

import java.util.Arrays;

public class LeeCode_985 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(sumEvenAfterQueries(new int[]{1, 2, 3, 4}, new int[][]{{1, 0}, {-3, 1}, {-4, 0}, {2, 3}})));
    }
    private static int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
        int n = nums.length, sum = 0, m = queries.length;
        for (int num : nums) {
            if ((num & 1) == 0) {
                sum += num;
            }
        }
        int[] ans = new int[m];
        for (int i = 0; i < m; i++) {
            int pre = nums[queries[i][1]];
            int next = pre + queries[i][0];
            nums[queries[i][1]] = next;
            if ((pre & 1) == 0){
                if ((next & 1) == 0){
                    sum += queries[i][0];
                }else {
                    sum -= pre;
                }
            }else {
                if ((next & 1) == 0){
                    sum += next;
                }
            }
            ans[i] = sum;
        }
        return ans;
    }
}
